package network;

import java.io.IOException;
import java.net.ServerSocket;

import main.Config;

public class ServerConnection {
	private int portNumber = 0;
	private ServerSocket serverSocket = null;
	
	public ServerConnection(Config config) {
		this(config.getPortNumber());
	}
	
	public ServerConnection(int portNumber) {
		this.portNumber = portNumber;
	}
	
	/**
	 * Open the connection, wait for the client and start a thread to handle it
	 */
	public void openConnection() {
		try {
			serverSocket = new ServerSocket(portNumber);
			HandleAcceptClient acceptTask = new HandleAcceptClient(serverSocket, this);
			Thread acceptThread = new Thread(acceptTask);
			acceptThread.start();
		}
		catch(IOException ex) {
			ex.printStackTrace();
		}
	}
	
	/**
	 * close the server socket
	 */
	public void closeConnection() {
		try {
			serverSocket.close();
		}
		catch(IOException ex) {
			ex.printStackTrace();
		}
	}
}
